/**
 * 
 */
angular.module('app.service.order', ['ngResource'])

//order
    .service('OrderService', ['$resource','ShopService','UserService','UserContactService', function ($resource,ShopService,UserService,UserContactService) {
        var resource = $resource(localhost + "order/:orderid");
        
        //获取列表
        this.getlist = function () {
            var promise_result = resource.query({});
            //
            var shopinfo_map = {};
            function fill_shopinfo(list, i) {
                if (i >= list.length) {
                    shopinfo_map = null;
                    return;
                }
                var item = list[i];
                if (shopinfo_map[item.shopid]) {
                    item.shopname = shopinfo_map[item.shopid];
                    fill_shopinfo(list, i + 1);
                } else {
                    ShopService.getshopbyid(item.shopid).$promise.then(function(data){
                        item.shopname = data.name;
                        shopinfo_map[item.shopid] = data.name;
                        fill_shopinfo(list, i + 1);
                    }, function(error){
                        console.log(error);
                    });
                }
            }
            promise_result.$promise.then(function(list){
                fill_shopinfo(list, 0);
            });
            //
            var userinfo_map = {};
            function fill_userinfo(list, i) {
                if (i >= list.length) {
                    userinfo_map = null;
                    return;
                }
                var item = list[i];
                if (userinfo_map[item.userid]) {
                    item.username = userinfo_map[item.userid];
                    fill_userinfo(list, i + 1);
                } else {
                    UserService.getuserbyid(item.userid).$promise.then(function(data){
                        item.username = data.name;
                        userinfo_map[item.userid] = data.name;
                        fill_userinfo(list, i + 1);
                    }, function(error){
                        console.log(error);
                    });
                }
            }
            promise_result.$promise.then(function(list){
                fill_userinfo(list, 0);
            });
            //
            var usercontact_map = {};
            function fill_usercontact(list, i) {
                if (i >= list.length) {
                    usercontact_map = null;
                    return;
                }
                var item = list[i];
                if (usercontact_map[item.contactid]) {
                    var data = usercontact_map[item.contactid];
                    item.contactname = data.name;
                    item.phone = data.phone;
                    item.addr = data.addr;
                    fill_usercontact(list, i + 1);
                } else {
                    UserContactService.getusercontactbyid(item.contactid).$promise.then(function(data){
                        item.contactname = data.name;
                        item.phone = data.phone;
                        item.addr = data.addr;
                        usercontact_map[item.contactid] = data;
                        fill_usercontact(list, i + 1);
                    }, function(error){
                        console.log(error);
                    });
                }
            }
            promise_result.$promise.then(function(list){
                fill_usercontact(list, 0);
            });
            //
            return promise_result;
        }
    }])
;